import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:flutter_common_utils/flutter_common_utils.dart';

import '../mvvm/base_view_model.dart';
import '../mvvm/base_widget_state.dart';

typedef AnimationBuilder = Widget Function(
  AnimationController controller,
  Widget child,
  AnimationParam animationParam,
);

abstract class BaseDialog extends BaseWidget {
  //优先级最高
  final bool? clickMaskDismiss = null;

  String get tag => identityHashCode(this).toString();

  BaseDialog({GlobalKey<BaseDialogState>? key}) : super(key: key ?? GlobalKey());

  // 通过 context 查找 State
  static T? of<T extends BaseDialogState>(BuildContext context) {
    return context.findAncestorStateOfType<T>();
  }

  // 提供一个方法来获取 State 实例
  BaseDialogState? get dialogState => (key as GlobalKey<State>?)?.currentState as BaseDialogState?;

  void show() {
    showFull();
  }

  void showFull({
    AnimationBuilder? animationBuilder,
    SmartAnimationType? animationType,
    SmartDialogController? controller,
    Alignment? alignment,
    bool? usePenetrate,
    bool? useAnimation,
    List<SmartNonAnimationType>? nonAnimationTypes,
    Duration? animationTime,
    Color? maskColor,
    Widget? maskWidget,
    bool? debounce,
    Duration? displayTime,
    bool? keepSingle,
    bool? permanent,
    bool? useSystem,
    bool? bindPage,
    BuildContext? bindWidget,
    Rect? ignoreArea,
    bool? clickMaskDismiss,
    SmartBackType? backType,
    FutureOr<bool> Function()? onBack,
  }) {
    SmartDialog.show(
      tag: tag,
      controller: controller,
      alignment: alignment,
      clickMaskDismiss: this.clickMaskDismiss ?? clickMaskDismiss,
      usePenetrate: usePenetrate,
      useAnimation: useAnimation,
      animationType: animationType,
      nonAnimationTypes: nonAnimationTypes,
      animationBuilder: animationBuilder,
      animationTime: animationTime,
      maskColor: maskColor,
      maskWidget: maskWidget,
      debounce: debounce,
      onDismiss: onDismiss,
      onMask: onMask,
      displayTime: displayTime,
      keepSingle: keepSingle,
      permanent: permanent,
      useSystem: useSystem,
      bindPage: bindPage,
      bindWidget: bindWidget,
      ignoreArea: ignoreArea,
      backType: backType,
      onBack: onBack,
      builder: dialogBuild,
    );
  }

  Widget dialogBuild(BuildContext context) => this;

  void dismiss() {
    SmartDialog.dismiss(tag: tag);
  }

  void onMask() {
    dialogState?.onMask();
  }

  void onDismiss() {
    dialogState?.onDismiss();
  }
}

abstract class BaseDialogState<T extends BaseDialog> extends BaseWidgetState<T> {
  String get dialogName => widget.runtimeType.toString();

  @override
  void initState() {
    super.initState();
    LogUtils.i("创建:$dialogName 对话框");
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    LogUtils.i("didChangeDependencies: $dialogName 对话框");
  }

  @override
  void didUpdateWidget(covariant T oldWidget) {
    super.didUpdateWidget(oldWidget);
    LogUtils.i("didUpdateWidget: $dialogName 对话框");
  }

  @override
  void deactivate() {
    super.deactivate();
    LogUtils.i("停用:$dialogName 对话框");
  }

  @override
  void dispose() {
    super.dispose();
    LogUtils.i("销毁$dialogName 对话框");
  }

  void dismiss() {
    widget.dismiss();
  }

  void onDismiss() {}

  void onMask() {}
}

abstract class BaseMvvmDialogState<T extends BaseDialog, VM extends BaseViewModel> extends BaseDialogState<T> {
  //主要的ViewMode
  late VM vm;

  BaseMvvmDialogState(VM? vvm) {
    initVm(vvm);
  }

  void initVm(VM? vvm) {
    if (vvm != null) {
      vm = vvm;
    }
  }


  @override
  void initState() {
    super.initState();
    if (vm.lifecycleOwner != this) {
      vm.isFirst = true;
    }
    vm.lifecycleOwner = this;
    vm.initState();
  }

  @override
  void dispose() {
    if (!vm.isDispose) {
      vm.dispose();
    }
    super.dispose();
  }

  @mustCallSuper
  @override
  void onDismiss() {
    super.onDismiss();
  }
}
